package com.heima.behavior.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.behavior.dto.EntryArticleDto;
import com.heima.behavior.entity.ApFollowBehavior;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/likes_behavior/")
public class ApBehaviorController {

    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    @Autowired
    private IApLikesBehaviorService likesBehaviorService;

    @Autowired
    private IApUnlikesBehaviorService unlikesBehaviorService;

    //数据回显：文章微服务端调用远程接口所需接口：点赞
    @PostMapping("/getIsLike")
    public ApLikesBehavior getIsLike(@RequestBody EntryArticleDto dto) {

        Long entryId = dto.getEntryId();

        Long articleId = dto.getArticleId();

        LambdaQueryWrapper<ApLikesBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApLikesBehavior::getEntryId, entryId).eq(ApLikesBehavior::getArticleId, articleId).orderByDesc(ApLikesBehavior::getCreatedTime);

        ApLikesBehavior apLikesBehavior = likesBehaviorService.list(query).get(0);

        return apLikesBehavior;
    }

    //数据回显：文章微服务端调用远程接口所需接口：不喜欢
    @PostMapping("/getUnLike")
    public ApUnlikesBehavior getUnLike(@RequestBody EntryArticleDto dto) {

        Long entryId = dto.getEntryId();

        Long articleId = dto.getArticleId();

        LambdaQueryWrapper<ApUnlikesBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApUnlikesBehavior::getEntryId, entryId).eq(ApUnlikesBehavior::getArticleId, articleId).orderByDesc(ApUnlikesBehavior::getCreatedTime);

        ApUnlikesBehavior apLikesBehavior = unlikesBehaviorService.list(query).get(0);

        return apLikesBehavior;
    }

    //数据回显：文章微服务端调用远程接口所需接口：是否关注
    @PostMapping("/getIsFollow")
    public ApFollowBehavior getIsFollow(@RequestBody EntryArticleDto dto) {

        Long entryId = dto.getEntryId();

        Long articleId = dto.getArticleId();

        LambdaQueryWrapper<ApFollowBehavior> query = new LambdaQueryWrapper<>();

        query.eq(ApFollowBehavior::getEntryId, entryId).eq(ApFollowBehavior::getFollowId, articleId).orderByDesc(ApFollowBehavior::getCreatedTime);

        ApFollowBehavior apLikesBehavior = followBehaviorService.list(query).get(0);

        return apLikesBehavior;
    }
}
